package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.OrderHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author YanJingren
 * @Date 2021/3/4
 * @Description com.atguigu.springcloud.controller
 */
@RestController
@Slf4j
@RequestMapping("/consumer/payment/hystrix")
@DefaultProperties(defaultFallback = "payment_global_fallback_method")
public class OrderHystrixController {

    @Resource
    private OrderHystrixService orderHystrixService;

    @GetMapping("/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") Integer id) {
        return orderHystrixService.paymentInfo_ok(id);
    }

    @GetMapping("/timeout2/{id}")
    @HystrixCommand
    public String paymentInfo_timeout2(@PathVariable("id") Integer id) {
        int age = 10 / 0;
        return orderHystrixService.paymentInfo_timeout(id);
    }

    public String payment_global_fallback_method() {
        return "Global异常处理信息，请稍后再试，(＃￣～￣＃)";
    }


    @GetMapping("/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeoutFallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    public String paymentInfo_timeout(@PathVariable("id") Integer id) {
//        int age = 10 / 0;
        return orderHystrixService.paymentInfo_timeout(id);
    }

    public String paymentTimeoutFallbackMethod(@PathVariable("id") Integer id) {
        return "我是消费者80，对方支付系统繁忙请10秒钟后重试或自己运行出错请检查自己，o(╥﹏╥)o";
    }

}
